package com.fxm.service.client.impl;

import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import com.fxm.returns.RespMap;
import com.fxm.service.client.WeChartClientService;
import com.fxm.service.feign.WeChartFeignClient;
import com.fxm.vo.result.SessionResult;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

/**
 * @author fxm
 * @version V1.0.0
 * @description: 微信官方接口 服务实现类
 * @date 2020/12/29 09:29
 */
@Service
@Slf4j
public class WeChartClientServiceImpl implements WeChartClientService {

    @Autowired
    private WeChartFeignClient weChartFeignClient;

    @Value("${appId}")
    private String appId;

    @Value("${appSecret}")
    private String appSecret;

    @Value("${grantType}")
    private String grantType;

    @Override
    public SessionResult getSessionKeyAndOpenId(String code) {
        SessionResult sessionResult = new SessionResult();
        try {
            String sessionKeyAndOpenId = weChartFeignClient.getSessionKeyAndOpenId(appId, appSecret, code, grantType);
            log.info("微信官方接口调用成功，object={}", sessionKeyAndOpenId);
            sessionResult = JSONUtil.toBean(sessionKeyAndOpenId, SessionResult.class);
        } catch (Exception e) {
            log.info("微信官方接口调用失败", e.getMessage());
        }
        return sessionResult;
    }
}
